﻿using System;
using System.Windows.Forms;

namespace Client.Forms
{
    public partial class SelectKRangeForm : Form
    {
        DateTime start;
        DateTime end;
        string freq;
        string adj;
        DataCenter.Models.StockInfo info;

        public SelectKRangeForm()
        {
            InitializeComponent();
        }

        private void SelectKRangeForm_Shown(object sender, EventArgs e)
        {
            textBox1.Focus();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!CheckDateTime())
                return;
            foreach(RadioButton i in freqPanel.Controls)
            {
                if (i.Checked)
                {
                    freq = (string)i.Tag;
                    break;
                }
            }
            foreach(RadioButton i in adjPanel.Controls)
            {
                if (i.Checked)
                {
                    adj = (string)i.Tag;
                    break;
                }
            }

            var p = new Panels.GGKLinePanel(info, freq, adj, start, end);
            Managers.Manager.panelM.AddPanel(p);

            this.Visible = false;
        }

        bool CheckDateTime()
        {
            string format = "yyyy-MM-dd";

            if (!DateTime.TryParseExact(textBox1.Text,format, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None,out start))
            {
                MessageBox.Show("起始日期输入错误。请用以下格式重新输入：\r\nyyyy-mm-dd","提示");
                textBox1.Focus();
                return false;
            }
            if (start > DateTime.Now)
            {
                MessageBox.Show("起始日期不能大于今天。请重新输入。", "提示");
                textBox1.Focus();
                return false;
            }

            if (!DateTime.TryParseExact(textBox2.Text, format, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out end))
            {
                MessageBox.Show("结束日期输入错误。请用以下格式重新输入：\r\nyyyy-mm-dd", "提示");
                textBox2.Focus();
                return false;
            }
            if (end > DateTime.Now)
            {
                MessageBox.Show("结束日期不能大于今天。请重新输入。", "提示");
                textBox2.Focus();
                return false;
            }

            if (start > end)
            {
                MessageBox.Show("起始日期不能大于结束日期。请重新输入。", "提示");
                textBox1.Focus();
                return false;
            }

            return true;
        }

        public void Open(DataCenter.Models.StockInfo info)
        {
            this.info = info;
            this.ShowDialog();
            textBox1.Focus();
        }
    }
}
